/* -----------------------------------------------------------------------
 *   Copyright (C) 2008 by Denjs
 *   dplsoft@pisem.net
 *   http://qdroid.berlios.de
 * -----------------------------------------------------------------------
 *   This program is free software licensed under GNU GPL License v.2 ; see main.cpp fo details
 *   Эта программа является свободным программным обеспечением; распространяемым под
 *   лицензией GNU General Public License v.2 (УНИВЕРСАЛЬНАЯ ОБЩЕСТВЕННАЯ ЛИЦЕНЗИЯ GNU версия 2.0) 
 * ----------------------------------------------------------------------- */
#include "t_TreeWidget.h"

//=========================================================
t_TreeWidget::t_TreeWidget(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	CoreQTreeWidget=this->findChild<QTreeWidget *>("treeWidget_1");



}

//=========================================================
t_TreeWidget::~t_TreeWidget()
{

}

//============================================================
// needs to have ability to be included to QList
t_TreeWidget::t_TreeWidget(const t_TreeWidget &other)// needs to be included to QList
{
	//qDebug()<<"t_TreeWidgetItemElement::t_TreeWidgetItemElement(t_TreeWidgetItemElement&):[0020]: other.Note1_inc =" << other.Note1_inc;
//	this->FileList.clear(); this->FileList+=(other.FileList);
//	this->CurrentElementCopy=other.CurrentElementCopy;

}
/**/

//============================================================
// needs to be included to QList
t_TreeWidget& t_TreeWidget::operator=(const t_TreeWidget &other)
{
	//qDebug()<<"t_TreeWidgetItemElement::operator=:[0033]: other.Note1_inc =" << other.Note1_inc;
//	this->FileList.clear();
//	this->FileList+=(other.FileList);
//	this->CurrentElementCopy=other.CurrentElementCopy;

	return *this;
}
//============================================================
/* virtual function to be redefined to make real class */
QString t_TreeWidget::std_in(QString Message)
{

	return "ok";
}


//===========================================================================
/* virtual function to be redefined to make real class */
t_TreeWidget* t_TreeWidget::copy()
{
	t_TreeWidget *El;
	El=new t_TreeWidget();
	*El=*this;
	return El;
}


//=========================================================
void t_TreeWidget::ItemClicked_rcv(QTreeWidgetItem *item, int column)
{
	t_TreeWidgetItem *TreeWidgetItem;
	TreeWidgetItem=new t_TreeWidgetItem(this);
	TreeWidgetItem->set_qwidgetitem(item);
	emit item_clicked(TreeWidgetItem, column);
}
//=========================================================
void t_TreeWidget::ItemDoubleClicked_rcv(QTreeWidgetItem *item, int column)
{
	t_TreeWidgetItem *TreeWidgetItem;
	TreeWidgetItem=new t_TreeWidgetItem(this);
	TreeWidgetItem->set_qwidgetitem(item);
	emit item_double_clicked(TreeWidgetItem, column);
}

//=========================================================
void t_TreeWidget::ItemActivated_rcv(QTreeWidgetItem *item, int column)
{
	t_TreeWidgetItem *TreeWidgetItem;
	TreeWidgetItem=new t_TreeWidgetItem(this);
	TreeWidgetItem->set_qwidgetitem(item);
	emit item_activated(TreeWidgetItem, column);

}
//=========================================================
void t_TreeWidget::ItemPressed_rcv(QTreeWidgetItem *item, int column)
{
	t_TreeWidgetItem *TreeWidgetItem;
	TreeWidgetItem=new t_TreeWidgetItem(this);
	TreeWidgetItem->set_qwidgetitem(item);
	emit item_activated(TreeWidgetItem, column);

}

//=========================================================
void t_TreeWidget::ItemSelectionChanged_rcv()
{
	emit item_selection_changed();
}

//=========================================================
void t_TreeWidget::CurrentItemChanged_rcv(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
	t_TreeWidgetItem *TreeWidgetItem_current;
	TreeWidgetItem_current=new t_TreeWidgetItem(this);
	TreeWidgetItem_current->set_qwidgetitem(current);

	t_TreeWidgetItem *TreeWidgetItem_previous;
	TreeWidgetItem_previous=new t_TreeWidgetItem(this);
	TreeWidgetItem_previous->set_qwidgetitem(previous);

	emit current_item_changed(TreeWidgetItem_current, TreeWidgetItem_previous);

}

//=========================================================
void t_TreeWidget::set_parent(QWidget *parent)
{
	setParent(parent);
}

//=========================================================
void t_TreeWidget::EditItem(t_TreeWidgetItem *item, int column)
{
	CoreQTreeWidget->editItem(item->CoreQTreeWidgetItem,column);
}

//=================================================================
int t_TreeWidget::expandItem(t_TreeWidgetItem *item)
{
	CoreQTreeWidget->expandItem(item->CoreQTreeWidgetItem);
	return 0;
}


//=[]========================================================
//==[операции с индексированным списком]=====================
//
//составляет список для работы с элементами по порядковому номеру//2008.12.03
int t_TreeWidget::index_items()
//t_FileList *t_TreeWidget::create_file_list()//создать объект t_FileList из текущего дерева
{
	IndexListForTreeWidgetItem.clear();
	QTreeWidgetItem *TmpQTreeWidgetItem;
	//QList<QTreeWidgetItem*> IndexListForTreeWidgetItem;
	int q;

	for (q=0;q< this->CoreQTreeWidget->topLevelItemCount();q++)
	 {
		IndexListForTreeWidgetItem+=scan_item_tree(this->CoreQTreeWidget->topLevelItem(q));
	 };

/*
	 for (int i = 0; i < TmpQTWList.size(); ++i)
	   {
	     qDebug() << "i="<< i <<" ID=" << TmpQTWList.at(i)->text(10) << " rel path=" << TmpQTWList.at(i)->text(11);
	   };
*/

	return IndexListForTreeWidgetItem.size();
}

//-----------------------------------------------------------
// ---- private one -----
QList<QTreeWidgetItem*> t_TreeWidget::scan_item_tree(QTreeWidgetItem* RootItem)
{
	QList<QTreeWidgetItem*> TmpQTWList;
	int q;
	TmpQTWList.append(RootItem);
	if (RootItem->childCount()>0)
	  {
		for (q=0;q<RootItem->childCount();q++)
		 {
			TmpQTWList+=scan_item_tree(RootItem->child(q));
		 };
	  };

	return TmpQTWList;
}

//-----------------------------------------------------------
//вернуть размер списка индексированных элементов//2008.12.03
int t_TreeWidget::indexed_items_count()
{
	return IndexListForTreeWidgetItem.size();
}

//-----------------------------------------------------------
//получить элемент по номеру//2008.12.03
t_TreeWidgetItem* t_TreeWidget::indexed_item(int _Position)
{
	t_TreeWidgetItem *TreeWidgetItem;
	TreeWidgetItem=new t_TreeWidgetItem(this);
	TreeWidgetItem->set_qwidgetitem(IndexListForTreeWidgetItem.at(_Position));
	return TreeWidgetItem;
}

//-----------------------------------------------------------
//получить индекс где заданное поле (колонка) равно значению
// если таких полей несколько, то выводится только порядковый номер первого
int t_TreeWidget::index_where_rowvalue_is(int _Row,QString _Value)
{
	 for (int i = 0; i < IndexListForTreeWidgetItem.size(); ++i)
	   {
		 if (IndexListForTreeWidgetItem.at(i)->text(_Row)==_Value)
			 return i;
	     //qDebug() << "i="<< i <<" ID=" << TmpQTWList.at(i)->text(10) << " rel path=" << TmpQTWList.at(i)->text(11);
	   };
	 return -1;
}



//=========================================================
//оболочка для topLevelItemCount()
int t_TreeWidget::get_top_level_item_count()
{
	return CoreQTreeWidget->topLevelItemCount();
}
//=========================================================
//оболочка для topLevelItem()
//получить корневой элемент списка
t_TreeWidgetItem* t_TreeWidget::get_top_level_item(int _Index)
{
	t_TreeWidgetItem *TmpTreeWidgetItem;
	TmpTreeWidgetItem=new t_TreeWidgetItem;
	TmpTreeWidgetItem->CoreQTreeWidgetItem=CoreQTreeWidget->topLevelItem(_Index);
	return TmpTreeWidgetItem;
}

//=========================================================
// для режима ручной обработки событий на дереве
int t_TreeWidget::enable_edittriggers(int _Mode)
{
	 if (_Mode==0)
		 CoreQTreeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
	    else
	     CoreQTreeWidget->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
}

//=========================================================
//
void t_TreeWidget::set_colomn_header_label(int _Column,QString _Label)
{
	this->CoreQTreeWidget->headerItem()->setText(_Column,_Label);
}

//=========================================================
void t_TreeWidget::set_header_labels(QString PropertyOrder)
{
	QStringList PropertyOrderList = QString(PropertyOrder).split(",");
	this->CoreQTreeWidget->setHeaderLabels(PropertyOrderList);
}

//=========================================================
void t_TreeWidget::set_colomn_width(int _Column,int _Width)
{
	this->CoreQTreeWidget->setColumnWidth(_Column, _Width);
}
//=========================================================
//выкрасить колонку в заданный цвет
int t_TreeWidget::set_column_background(int _Column,QString _RRGGBBColorString)
{
	if (IndexListForTreeWidgetItem.size()<1) index_items();

	QBrush Brush=QBrush(QColor(_RRGGBBColorString));
	for (int i = 0; i < IndexListForTreeWidgetItem.size(); ++i)
	   {
		 (IndexListForTreeWidgetItem.at(i))->setBackground(_Column, Brush);
	   };
	return 0;
}

int t_TreeWidget::sortByColumn(int _Column, int _Order)//2009.09.02 added 
{
	/*
		Qt::AscendingOrder = 0   AAAA->ZZZZ
		Qt::DescendingOrder = 1   ZZZZ->AAAA
	*/
	Qt::SortOrder order;
	if (_Order==0) order=Qt::AscendingOrder; else order=Qt::DescendingOrder;
	this->CoreQTreeWidget->sortItems (_Column, order);

// setColumnWidth(_Column, _Width);
	
	
}

//=========================================================
// работа с кнопками
//вернуть ссылку на кнопку с заданным именем.
QPushButton* t_TreeWidget::get_button(QString PushButonName)
{
	QPushButton *PushButtonObject;
	PushButtonObject=this->findChild<QPushButton *>(PushButonName);
	return PushButtonObject;
}
int  t_TreeWidget::hide_all_buttons()
{
	(this->findChild<QPushButton *>("pushButton_8"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_1"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_2"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_3"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_4"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_5"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_6"))->setVisible(false);
	(this->findChild<QPushButton *>("pushButton_7"))->setVisible(false);
	return 0;
}

//=================================================================
//поведение выбора см enum QAbstractItemView::SelectionBehavior
/*
QAbstractItemView::SelectItems 		0 	Selecting single items.
QAbstractItemView::SelectRows 		1	Selecting only rows.
QAbstractItemView::SelectColumns	2	Selecting only columns.
*/
int t_TreeWidget::set_selection_behavior(int _Mode)
{
	QAbstractItemView::SelectionBehavior Behavior;
	if (_Mode==0) Behavior=QAbstractItemView::SelectItems;
	if (_Mode==1) Behavior=QAbstractItemView::SelectRows;
	if (_Mode==2) Behavior=QAbstractItemView::SelectColumns;

	this->CoreQTreeWidget->setSelectionBehavior(Behavior);
	return 0;
}
//=================================================================
//установить размер окна
int t_TreeWidget::set_size(int _width,int _height)
{
	this->resize(_width,_height);
}

//передвинуть окно
int t_TreeWidget::set_pos(int _x,int _y)
{
	this->move(_x,_y);
}

